package com.shopping.controller;

import com.shopping.common.Result;
import com.shopping.entity.Message;
import com.shopping.service.MessageService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import com.shopping.util.SecurityUtils;

@RestController
@RequestMapping("/api/message")
public class MessageController {

    @Resource
    private MessageService messageService;

    @PostMapping
    public Result<Boolean> publishMessage(@RequestBody Message message) {
        // 设置当前用户ID
        Long userId = SecurityUtils.getCurrentUserId();
        if (userId == null) {
            return Result.error("用户未登录");
        }
        message.setUserId(userId);
        boolean success = messageService.publishMessage(message);
        return success ? Result.success(true) : Result.error("发布留言失败");
    }

    @PutMapping("/{id}")
    public Result<Boolean> updateMessage(@PathVariable Long id, @RequestBody Message message) {
        // 设置当前用户ID
        Long userId = SecurityUtils.getCurrentUserId();
        if (userId == null) {
            return Result.error("用户未登录");
        }

        // 检查留言是否存在且属于当前用户
        Message existingMessage = messageService.getById(id);
        if (existingMessage == null) {
            return Result.error("留言不存在");
        }
        if (!existingMessage.getUserId().equals(userId)) {
            return Result.error("无权修改此留言");
        }

        message.setId(id);
        message.setUserId(userId);
        message.setUsername(existingMessage.getUsername()); // 保持原有用户名
        message.setCreateTime(existingMessage.getCreateTime()); // 保持原有创建时间
        message.setIsReplied(existingMessage.getIsReplied()); // 保持原有回复状态
        message.setReply(existingMessage.getReply()); // 保持原有回复内容
        message.setReplyTime(existingMessage.getReplyTime()); // 保持原有回复时间

        boolean success = messageService.updateById(message);
        return success ? Result.success(true) : Result.error("更新留言失败");
    }

    @PutMapping("/reply")
    public Result<Boolean> replyMessage(@RequestParam Long messageId, @RequestParam String reply) {
        boolean success = messageService.replyMessage(messageId, reply);
        return success ? Result.success(true) : Result.error("回复留言失败");
    }

    @GetMapping("/list")
    public Result<List<Message>> getAllMessages() {
        return Result.success(messageService.getAllMessages());
    }

    @GetMapping("/user/current")
    public Result<List<Message>> getCurrentUserMessages() {
        Long userId = SecurityUtils.getCurrentUserId();
        return Result.success(messageService.getUserMessages(userId));
    }

    @GetMapping("/user/{userId}")
    public Result<List<Message>> getUserMessages(@PathVariable Long userId) {
        return Result.success(messageService.getUserMessages(userId));
    }

    @DeleteMapping("/{id}")
    public Result<Boolean> deleteMessage(@PathVariable Long id) {
        return Result.success(messageService.removeById(id));
    }
}